﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;
using System.Web.Security;

public partial class Login : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Cricket.LinqModel.Client client;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            client = new Client(_dataObject);
            List<ClientLoginResult> loginClient = new List<ClientLoginResult>();
            loginClient = client.ClientLogin(username, password);
            
            if (loginClient.Count > 0)
            {
                LoggedInUser user = new LoggedInUser();
                user.ClientId = Convert.ToInt32(loginClient[0].Id);
                user.FullName = loginClient[0].FullName;
                user.AdminName = loginClient[0].AdminFullName;
                user.AdminId = Convert.ToInt32(loginClient[0].AdminId);

                HttpCookie cookie = new HttpCookie("Client");
                
                //Cookie Data
                cookie["CommonRate"] = loginClient[0].CommonRate.ToString();
                cookie["ClientId"] = loginClient[0].Id.ToString();
                cookie["FullName"] = loginClient[0].FullName;
                cookie["AdminName"] = loginClient[0].AdminFullName;
                cookie["AdminId"] = loginClient[0].AdminId.ToString();
                cookie["Limit"] = loginClient[0].Limit.ToString();
                cookie["MatchTempLimit"] = loginClient[0].MatchTemplimit.ToString();

                cookie["Commission"] = loginClient[0].CommissionRate;
                cookie["ActualLimit"] = loginClient[0].ActualLimit.ToString();
                cookie["MinLimit"] = loginClient[0].MinLimit.ToString();
                cookie["MaxLimit"] = loginClient[0].MaxLimit.ToString();
                cookie["IsLogin"] = loginClient[0].IsLogin.ToString();
                cookie.Expires = DateTime.Now.AddMinutes(15);
                Response.Cookies.Add(cookie);
                client = new Client(_dataObject);
                client.Id = Convert.ToInt32(cookie["ClientId"]);
                client.IsLogin = Convert.ToBoolean(cookie["IsLogin"]); 
                
                if (client.IsLogin == true)
                {
                    
                    return;
                }
                else
                {
                    //Response.Write("2");
                    client.IsLogin = true;
                    client.Id = Convert.ToInt32(cookie["ClientId"]);
                    Response.Write(client.Id);
                    client.UpdateIsLoginForClient();
                    Response.Write("Test");
                    Response.Redirect("Dashboard.aspx");
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }
    }
}